﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text.RegularExpressions;


namespace CCPSIP.vCard
{
    /// <summary>vCard 名片交換: VCF 檔已下載確認</summary>
    
    public class vcf_download_ack : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/plain";
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            DateTime reqTime = DateTime.Now;
            string resultStr = "";


            try
            {
                //【01】取得POST參數，驗證身分
                //
                string idHeader = req.Form["idHeader"];
                string checkcode = req.Form["checkcode"];
                string vCardKey = req.Form["vCardKey"];//vCard Unique Key (Format="Owner_yyyyMMdd_HHmmss")

                SipUserInfo userInfo = SipKit.GetUserInfo(idHeader, checkcode, req);
                if (userInfo == null)
                {
                    throw new Exception("Error_01: Cannot identify this device");//無法識別此裝置
                }


                //【02】檢查 vCardKey 格式
                //
                string keyPattern = string.Format("^{0}_{1}$",
                  SipKit.kSIPNumPattern.TrimStart('^').TrimEnd('$'),
                  "2[0-9]{3}[01][0-9][0-3][0-9]_[0-2][0-9][0-5][0-9][0-5][0-9]");
                if (!Regex.IsMatch(vCardKey, keyPattern))
                {
                    throw new Exception("Error_02: vCardKey format error");//vCardKey格式錯誤
                }

                int spIndex = vCardKey.IndexOf("_");
                string vCardOwner = vCardKey.Substring(0, spIndex);
                DateTime? vCardTime = ChrisKit.ParseDateTime(vCardKey.Substring(spIndex + 1), "yyyyMMdd_HHmmss");
                if (!vCardTime.HasValue)
                {
                    throw new Exception("Error_02: vCardKey format error");//vCardKey格式錯誤
                }


                //【03】嘗試寫入群組交換時間
                //
                int updateCount = OracleKitVCardGE.UpdateDLTime(vCardOwner, vCardTime.Value, userInfo.SIP_Num);


                //【04】嘗試寫入個人交換時間
                //
                if (updateCount == 0)
                {
                    updateCount = OracleKitVCard.UpdateDLTime(vCardOwner, vCardTime.Value, userInfo.SIP_Num);
                }


                //【99】回傳結果
                //
                resultStr = "OK";
            }
            catch (Exception ex)
            {
                resp.ContentType = "application/xml";
                resultStr = ChrisKit.Ex2Xml(ex, false);
            }
            finally
            {
                int contentLength = resp.ContentEncoding.GetByteCount(resultStr);
                resp.AppendHeader("Content-Length", contentLength.ToString());
                resp.Write(resultStr);
                resp.Flush();
                resp.Close();
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}
